<?php

namespace Common\Model;

/**
 * HotelDailyReportModel.class实例模型
 *
 * @author songanwei
 * @date 2016-1-19
 */
class HotelDailyReportModel extends BaseModel {

    protected $trueTableName = 'tb_hotel_daily_report';

    protected function _before_insert(&$data, $options) {
        // parent::_before_insert($data, $options);
    }

    protected function _before_update(&$data, $options) {
        // parent::_before_update($data, $options);
    }

    /**
     * 返回客栈查询日期段内的数据
     * @Modify songanwei 2016-01-19
     * @param integer $hotelId 客栈ID
     * @param date $startDate 查询开始日期
     * @param date $endDate 查询结束日期
     * @return type
     */
    public function getInfosByHotelDate($hotelId, $startDate, $endDate) {
        $where['hotelentity_id'] = $hotelId;
        if ($endDate) {
            $where['date'] = array(array('egt', $startDate), array('elt', $endDate), 'AND');
        } else {
            $where['date'] = $startDate;
        }
        return $this->where($where)->select();
    }

    /**
     * 添加数据
     * @modify songanwei 2016-01-19
     * @param integer $hotelId  客栈ID
     * @param date $date  日期
     * @param integer $receipt  已收款
     * @param integer $refund  已退款
     * @param integer $deposit  押金
     * @param integer $refundDeposit  已退押金
     * @param integer $roomNumber  房间数量
     * @return type
     */
    public function addInfo($hotelId, $date, $receipt, $refund, $deposit, $refundDeposit = 0, $roomNumber = 0) {
        $options = array(
            'hotelentity_id' => $hotelId,
            'date' => $date,
            'receipt' => $receipt,
            'refund' => $refund,
            'deposit' => $deposit,
            'refund_deposit' => $refundDeposit,
            'roomnumber' => $roomNumber
        );
        return $this->add($options);
    }

    /**
     * 更新数据
     * @modify songanwei 2016-01-19
     * @modify trainyao  2016-01-21     修改不一定更新房间数量
     * @param integer $hotelId 客栈ID
     * @param date $date 日期 date('Y-m-d')
     * @param integer $receipt 已收款
     * @param integer $refund 已退款
     * @param integer $deposit 押金
     * @param integer $refundDeposit 已退押金
     * @param integer $roomNumber 房间数量
     * @return type
     */
    public function saveInfo($hotelId, $date, $receipt, $refund, $deposit, $refundDeposit = 0, $roomNumber = null) {
        $where = array(
            'hotelentity_id' => $hotelId,
            'date' => $date
        );
        $options = array(
            'receipt' => $receipt,
            'refund' => $refund,
            'deposit' => $deposit,
            'refund_deposit' => $refundDeposit,
        );
        if (!is_null($roomNumber)) {
            $options['roomnumber'] = $roomNumber;
        }
        $result = $this->where($where)->save($options);
        return $result === false ? false : true;
    }

}
